from flask import jsonify
import src.data_struct as data_struct


def log_button_action(button_id, state):
    """
    记录按钮动作
    """
    print(f"按钮{button_id}状态: {state}", flush=True)

def handle_button1(state):
    """
    处理按钮1的响应
    """
    
    try:
        log_button_action(1, state)
        data_struct.button_1 = state
        return jsonify({
            'status': 'success',
            'message': f'按钮1状态已更新为: {state}'
        })
    except Exception as e:
        return jsonify({
            'status': 'error',
            'message': str(e)
        }), 500

def handle_button2(state):
    """
    处理按钮2的响应
    """
    try:
        log_button_action(2, state)
        data_struct.button_2 = state
        return jsonify({
            'status': 'success',
            'message': f'按钮2状态已更新为: {state}'
        })
    except Exception as e:
        return jsonify({
            'status': 'error',
            'message': str(e)
        }), 500

def handle_button3(state):
    """
    处理按钮3的响应
    """
    try:
        log_button_action(3, state)
        data_struct.button_3 = state
        return jsonify({
            'status': 'success',
            'message': f'按钮3状态已更新为: {state}'
        })
    except Exception as e:
        return jsonify({
            'status': 'error',
            'message': str(e)
        }), 500

def handle_button4(state):
    """
    处理按钮4的响应
    """
    try:
        log_button_action(4, state)
        data_struct.button_4 = state
        return jsonify({
            'status': 'success',
            'message': f'按钮4状态已更新为: {state}'
        })
    except Exception as e:
        return jsonify({
            'status': 'error',
            'message': str(e)
        }), 500 